﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Microsoft.HomeServer.Controls;
using Umbrella.Net.Contracts;
using Umbrella.Whs.SharePoint;
using Umbrella.Whs.WhsAddin.Controls;

namespace Umbrella.Whs.WhsAddin.Wizards.SharePointInstallation
{
    internal partial class Step20_SelectLanguagePacks : WizardPageClientBannerTop
    {
        private SharePointInstallationWizard ParentWizard2
        {
            get { return (SharePointInstallationWizard)this.ParentWizard; }
        }

        public Step20_SelectLanguagePacks()
        {
            InitializeComponent();

            // add images
            this.imageList.Images.Add("de", SharePoint.FlagIcons.de);
            this.imageList.Images.Add("en", SharePoint.FlagIcons.en);
        }

        public override void StartRealWork()
        {
            base.StartRealWork();

            // fill list
            this.langs.Rows.Clear();
            this.langs.SuspendLayout();

            var currentLcid = Thread.CurrentThread.CurrentUICulture.LCID;

            foreach (var ip in this.ParentWizard2.InstallationFiles)
            {
                var lng = (
                    from l in this.ParentWizard2.Languages 
                    where l.LCID == ip.LanguageLCID 
                    select l
                    ).First();

                var row = new DataGridViewRow();

                row.Tag = ip;
                row.ReadOnly = (InstallationManager.RootLanguageLCID == lng.LCID);

                row.Cells.Add(new DataGridViewCheckBoxCell() { Value = (row.ReadOnly || (currentLcid == lng.LCID)) });
                row.Cells.Add(new DataGridViewImageCell() { Value = this.imageList.Images[lng.ISO] });
                row.Cells.Add(new DataGridViewTextBoxCell() { Value = lng.Title });

                this.langs.Rows.Add(row);
            }

            this.langs.Sort(this.langs.Columns[2], ListSortDirection.Ascending);
            this.langs.ResumeLayout();
        }

        private void Step20_SelectLanguagePacks_OkButtonClick(object sender, EventArgs e)
        {
            // store selected
            this.ParentWizard2.SelectedInstallationFiles = (
                from DataGridViewRow r in this.langs.Rows
                where (bool)r.Cells[0].Value
                select (SharePointInstallationFile)r.Tag
                ).ToList();

            // goto next page
            SetCurrentPage<Step30_Download>();
        }

        private void Step20_SelectLanguagePacks_CancelButtonClick(object sender, EventArgs e)
        {
            if (QMessageBox.Show(
                Resources.txtCancelVerifyText,
                Resources.txtCancelVerifyTitle,
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Question
                ) != DialogResult.Yes)
            {
                return;
            }
            
            this.CancelButtonEnabled = false;

            // close wizard
            this.ParentWizard.Close();
        }
    }
}
